package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "套餐相关接口")
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    //1：套餐-分页查询
    @GetMapping("/page")
    @ApiOperation("套餐-分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐-分页查询:{}",setmealPageQueryDTO);
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    //2:套餐-新增套餐
    @PostMapping
    @ApiOperation("套餐-新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result addTao(@RequestBody SetmealDTO setmealDTO){
        log.info("套餐-新增套餐:{}",setmealDTO);
        setmealService.addTao(setmealDTO);
        return Result.success();
    }
    //3:套餐-根据id查询套餐
    @GetMapping("/{id}")
    @ApiOperation("套餐-根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("套餐-根据id查询套餐:{}",id);
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }
    //4:套餐-批量删除菜品
    @DeleteMapping
    @ApiOperation("套餐-批量删除菜品")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteBatch(@RequestParam List<Long> ids){//可以把字符串转换为list集合
        log.info("套餐-批量删除菜品:{}",ids);
        setmealService.deleteBatch(ids);
        return Result.success();
    }
    //5:套餐-修改菜品
    @PutMapping
    @ApiOperation("套餐-修改菜品")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result putGai(@RequestBody SetmealDTO setmealDTO){
        log.info("套餐-修改菜品:{}",setmealDTO);
        setmealService.putGai(setmealDTO);
        return Result.success();
    }
    //6:套餐-起售停售
    @PostMapping("/status/{status}")
    @ApiOperation("套餐-起售停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("套餐-起售停售:是否起售状态:{},id：{}",status,id);
        setmealService.startOrStop(status,id);
        return Result.success();
    }

}
